/**
 * 
 */
package com.hfjyz.registration.service;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;

import com.hfjyz.registration.command.ManagerEditCommand;
import com.hfjyz.registration.command.ManagerListCommand;
import com.hfjyz.registration.dao.AdminDao;
import com.hfjyz.registration.model.Admin;
import com.hfjyz.registration.util.CryptUtil;

/**
 *
 */
public class ManagerServiceImpl extends AbstractServiceImpl implements
		ManagerService {
	private AdminDao adminDao;

    @SuppressWarnings("rawtypes")
    @Override
    public Map initEdit(ManagerListCommand cmd, Errors errors) throws Exception {
        if (cmd != null && cmd.getManagerName() != null) {
            Admin admin = adminDao.get(cmd.getManagerName());
            if (admin != null) {
                cmd.setId(admin.getId());
                cmd.setManagerName(admin.getAdministratorId());
            }
        }
        return null;
    }

    @Override
    public void editOk(HttpServletRequest request, ManagerEditCommand cmd,
            BindException errors) throws Exception {

        Admin admin = adminDao.get(cmd.getAdminId());
        if (admin != null) {
            errors.rejectValue("adminId", "adminId.error", "用户帐号已存在!");
            return;
        }
        admin = new Admin();
        admin.setAdministratorId(cmd.getAdminId());
        admin.setPassword(CryptUtil.encrypt(cmd.getPassword()));

        if (admin.getId() == null) {
            adminDao.add(admin);
        } else {
            adminDao.update(admin);
        }
    }

    @Override
    public void filter(HttpServletRequest request,
            HttpServletResponse response, ManagerListCommand cmd,
            BindException errors) throws Exception {
        cmd.setEid(null);
        List<Admin> managers = adminDao.filter(cmd.getManagerName());
        cmd.setResult(managers);
    }

    @Transactional
    @Override
    public void delete(HttpServletRequest request, ManagerListCommand cmd,
            BindException errors) throws Exception {
        if (cmd.getEid() != null) {
            adminDao.delete(adminDao.getInfoById(cmd.getEid()));
        }
    }

    @SuppressWarnings("rawtypes")
    @Override
    public Map initManager(ManagerListCommand cmd, Errors errors)
            throws Exception {
        cmd.setResult(adminDao.listAll());
        return null;
    }

    /**
     * @return the ManagerDao
     */
    public AdminDao getAdminDao() {
        return adminDao;
    }

    /**
     * @param ManagerDao
     *            the ManagerDao to set
     */
    public void setAdminDao(AdminDao ManagerDao) {
        this.adminDao = ManagerDao;
    }

}
